package org.hxyjs.dao.impl;

import org.hxyjs.dao.AdminDao;
import org.hxyjs.entity.Admin;
import org.hxyjs.mapper.AdminMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("adminDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdminDaoImpl implements AdminDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdminMapper adminMapper;

    @Override
    public List<Admin> getAdminByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) adminMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return adminMapper.getAdminByPage(map);
    }

    @Override
    public Admin getAccountByAccountName(String aname) throws Exception {
        return adminMapper.getAccountByAccountName(aname);
    }


    public List<Admin> getAllAdmins() throws Exception {
        return adminMapper.selectByExample(null);
    }

    @Override
    public Admin getAdminByAno(String ano) throws Exception {
        return adminMapper.selectByPrimaryKey(ano);
    }


    @Override
    public Integer deleteAdminByAno(String ano) {
        return adminMapper.deleteByPrimaryKey(ano);
    }

    @Override
    public Integer updateAdminByAno(Admin admin) {

        return adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    public Integer addAdmin(Admin admin) {
        admin.setAno(initId());
        return adminMapper.insertSelective(admin);
    }

    public String initId() {
        String maxAno = adminMapper.getMaxAno();
        return GenNumTools.initId("", 4, maxAno);
    }

}